Page({
  data: {
    rankList: []
  },
  
  onLoad: function() {
    this.getRankList();
  },
  
  getRankList: function() {
    const db = wx.cloud.database();
    
    
    db.collection('answer_record')
      .orderBy('totalScore', 'desc')
      .get()
      .then(res => {
        if (res.data && res.data.length > 0) {
          // 筛选每个用户的最高分记录
          const userMap = {};
          res.data.forEach(item => {
            if (!userMap[item.openid] || item.totalScore > userMap[item.openid].totalScore) {
              userMap[item.openid] = {
                nickname: item.nickname,
                avatar: item.avatarUrl,
                totalScore: item.totalScore
              };
            }
          });
          
          // 转换为数组并按分数排序
          const rankList = Object.values(userMap)
            .sort((a, b) => b.totalScore - a.totalScore)
            .slice(0, 7);
          this.setData({
            rankList: rankList
          });
        } else {
          this.setData({
            rankList: []
          });
        }
      })
      .catch(err => {
        console.error('获取排行榜失败:', err);
        wx.showToast({
          title: '获取排行榜失败',
          icon: 'none'
        });
      });
  }
})